<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="幼儿姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :maxLength="10" placeholder="请输入幼儿姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿学号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentNo">
              <a-input v-model="model.studentNo" :maxLength="30" placeholder="请输入幼儿学号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list" v-model="model.classId" dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birthday">
              <j-date placeholder="请选择出生日期" v-model="model.birthday"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择幼儿性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idType">
              <j-dict-select-tag type="list" v-model="model.idType" dictCode="certificate_type" placeholder="请选择证件类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿证件号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNo">
              <a-input v-model="model.idNo" :maxLength="28" placeholder="请输入证件号码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="guardianName">
              <a-input v-model="model.guardianName" :maxLength="10" placeholder="请输入家长姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入家长联系方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="guardianIdType">
              <j-dict-select-tag type="list"  v-model="model.guardianIdType" dictCode="certificate_type" placeholder="请选择家长证件类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长证件号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="guardianIdNo">
              <a-input v-model="model.guardianIdNo" :maxLength="28" placeholder="请输入家长证件号码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家庭地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" :maxLength="250" placeholder="请输入家庭地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿照片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="photo">
              <j-image-upload :number="1" isMultiple   v-model="model.photo" ></j-image-upload>
              <p>请上传 1 张<span style="margin:0 5px;color:#f00;">2寸</span>照片</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="招生年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="year">
              <a-input v-model="model.year" :maxLength="10" placeholder="请输入招生年度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="student_status" placeholder="请选择幼儿状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿接送人" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-multi-select-tag
                v-model="model.selectedpickups"
                dictCode="sk_pickup,name,id,del_flag=0"
                placeholder="请选择幼儿接送人">
              </j-multi-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幼儿监护人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag
                v-model="model.selectedguardians"
                dictCode="sys_user,realname,id,del_flag=0 and user_identity=4"
                placeholder="请选择幼儿监护人">
              </j-multi-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { validateMobile } from '@/utils/rules.js'
  import {queryById} from "@/api/management.js";
  export default {
    name: 'SkStudentForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           idType: [
              { required: true, message: '请输入幼儿证件类型!'},
           ],
           idNo: [
              { required: true, message: '请输入幼儿证件号码!'},
           ],
           studentNo: [
              { required: true, message: '请输入幼儿学号!'},
           ],
           name: [
              { required: true, message: '请输入幼儿姓名!'},
           ],
           sex: [
              { required: true, message: '请输入幼儿性别!'},
           ],
           birthday: [
              { required: true, message: '请输入出生日期!'},
           ],
           phone: [
              { required: true, message: '请输入家长联系方式!'},
              { validator: validateMobile }
           ],
           photo: [
              { required: true, message: '请上传幼儿照片!'}
           ],
           classId: [
              { required: true, message: '请输入班级!'},
           ],
           year: [
              { required: true, message: '请输入招生年度!'},
           ],
           guardianName: [
              { required: true, message: '请输入家长姓名!'},
           ],
           guardianIdType: [
              { required: true, message: '请输入家长证件类型!'},
           ],
           guardianIdNo: [
              { required: true, message: '请输入家长证件号码!'},
           ],
           status: [
              { required: true, message: '请输入幼儿状态!'},
           ],
          //  selectedpickups: [
          //     { required: true, message: '请选择幼儿接送人!'},
          //  ],
          //  selectedguardians: [
          //     { required: true, message: '请选择幼儿监护人!'},
          //  ],
        },
        url: {
          add: "/management/skStudent/add",
          edit: "/management/skStudent/edit",
          queryId: "/management/skStudent/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>